/*
 * 文件名：DateTimeFormatHandler.java
 * 版权：亚信联创版权所有
 * 描述：TODO
 * 修改人：yuanze
 * 修改时间：2014年12月1日
 * 修改内容：TODO
 */
package com.ailk.bigdata.etl.realstream.server.service.impl;

import io.netty.buffer.ByteBuf;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ailk.bigdata.etl.realstream.server.service.FormatHandler;

/**
 * @description TODO
 * @author [yuanze]
 * @version [版本号,2014年12月1日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class StringFormatHandler extends FormatHandler
{
	private Logger logger = LoggerFactory.getLogger(StringFormatHandler.class);

	/**
	 * @see com.ailk.bigdata.etl.realstream.server.service.FormatHandler#handleRequest(io.netty.buffer.ByteBuf,
	 *      int, java.lang.StringBuilder)
	 */
	@Override
	public void handleRequest(ByteBuf byteBuf, StringBuilder content)
	{

		String returnVal = "";
		try
		{
			int readLength = 0;
			if (-1 == length)
			{
				readLength = byteBuf.readUnsignedShort();
			}
			else
			{
				readLength = length;
			}
			byte[] cbite = byteBuf.readBytes(readLength).array();
			returnVal = new String(cbite, "UTF-8");

			String hexString = toHexString(cbite);
			if (hexString.matches("^[Ff]+$"))
			{
				returnVal = "";
			}

			if (logger.isDebugEnabled())
			{
				logger.debug("字节码[" + Arrays.toString(cbite) + "]16进制码[" + hexString + "]字符串[" + new String(cbite, "UTF-8") + "]");
			}

			cbite = null;
			hexString = null;
		}
		catch (UnsupportedEncodingException e)
		{
			logger.error("实时流读取String异常", e);
		}
		content.append(returnVal.trim());
		if (getSuccessor() != null)
		{
			content.append(separator);
			getSuccessor().handleRequest(byteBuf, content);
		}
		returnVal = null;
	}

}
